﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework;

namespace LloydGame
{
    public partial class frmEditor : Form
    {
        protected ControlCollection m_controlCollection;
        protected GameWorld m_gameWorld;
        protected GameObject m_currentGameObject;
        protected String m_spawnObjectType;
        protected String m_powerUp_Coin_Parameter;
        protected bool m_spawnObjectStaticFlag;

        public frmEditor(GameWorld p_gameWorld)
        {
            // Put all numeric controls in a collection
            m_controlCollection = new ControlCollection(this);
            m_controlCollection.Add(nudPositionX);
            m_controlCollection.Add(nudPositionY);
            m_controlCollection.Add(nudPositionZ);
            m_controlCollection.Add(nudRotationX);
            m_controlCollection.Add(nudRotationY);
            m_controlCollection.Add(nudRotationZ);
            m_controlCollection.Add(nudScaleX);
            m_controlCollection.Add(nudScaleY);
            m_controlCollection.Add(nudScaleZ);

            m_gameWorld = p_gameWorld;
            InitializeComponent();
            UpdateFormControls();

            // Add the same event handler for every object in the container
            /*
            foreach (Control con in m_controlCollection)
            {
                ((NumericUpDown)con).ValueChanged += new System.EventHandler(this.nudValue_ValueChanged);
            }
            */
        }

        private void frmEditor_Load(object sender, EventArgs e)
        {
            this.Left = 0;
            this.Top = 0;
        }

        public void UpdateObject(GameObject p_gameObject)
        {
            // Set reference to the new object
            m_currentGameObject = p_gameObject;

            // Update all form controls related to object handling
            UpdateFormControls();
        }

        private void UpdateFormControls()
        {
            foreach (Control con in m_controlCollection)
            {
                // Enable controls if not null
                con.Enabled = (m_currentGameObject == null) ? false : true;
            }

            // Update values for the object
            if (m_currentGameObject != null)
            {
                lblPositionX.Text = ((float)m_currentGameObject.body.Position.X).ToString();
                lblPositionY.Text = ((float)m_currentGameObject.body.Position.Y).ToString();
                lblPositionZ.Text = ((float)m_currentGameObject.body.Position.Z).ToString();
                lblRotationX.Text = ((float)m_currentGameObject.rotation.X).ToString();
                lblRotationY.Text = ((float)m_currentGameObject.rotation.Y).ToString();
                lblRotationZ.Text = ((float)m_currentGameObject.rotation.Z).ToString();
                lblScaleX.Text = ((float)m_currentGameObject.scale.X).ToString();
                lblScaleY.Text = ((float)m_currentGameObject.scale.Y).ToString();
                lblScaleZ.Text = ((float)m_currentGameObject.scale.Z).ToString();

            }
        }

        private void nudValue_ValueChanged(object sender, EventArgs e)
        {
            NumericUpDown nudTemp = (NumericUpDown)sender;
            Microsoft.Xna.Framework.Vector3 vecTemp;

            if (nudTemp.Name.StartsWith("nudPosition"))
            {
                vecTemp = m_currentGameObject.body.Position;
            }
            else if (nudTemp.Name.StartsWith("nudRotation"))
            {
                //vecTemp = m_currentGameObject.
            }
            else if (nudTemp.Name.StartsWith("nudScale"))
            {
                vecTemp = m_currentGameObject.scale;
            }

            if (nudTemp.Name.EndsWith("X"))
            {
                vecTemp.X = (float)nudTemp.Value;
            }
            else if (nudTemp.Name.EndsWith("Y"))
            {
                vecTemp.Y = (float)nudTemp.Value;
            }
            else if (nudTemp.Name.EndsWith("Z"))
            {
                vecTemp.Z = (float)nudTemp.Value;
            }
        }

        private void btnPositionChange_Click(object sender, EventArgs e)
        {
            if (btnPositionChange.Text.Equals("cancel"))
            {
                nudPositionX.Visible = false;
                nudPositionY.Visible = false;
                nudPositionZ.Visible = false;
                btnPositionChange.Text = "change";
                btnUpdatePosition.Visible = false;

                nudRotationX.Visible = false;
                nudRotationY.Visible = false;
                nudRotationZ.Visible = false;
                btnSpawn.Visible = false;
            }
            else
            {
                if (m_currentGameObject != null)
                {
                    nudPositionX.Value = (decimal)m_currentGameObject.body.Position.X;
                    nudPositionY.Value = (decimal)m_currentGameObject.body.Position.Y;
                    nudPositionZ.Value = (decimal)m_currentGameObject.body.Position.Z;
                    nudPositionX.Visible = true;
                    nudPositionY.Visible = true;
                    nudPositionZ.Visible = true;
                    btnUpdatePosition.Visible = true;
                    btnPositionChange.Text = "cancel";

                    nudRotationX.Value = (decimal)MathHelper.ToDegrees(m_currentGameObject.rotation.X);
                    nudRotationY.Value = (decimal)MathHelper.ToDegrees(m_currentGameObject.rotation.Y);
                    nudRotationZ.Value = (decimal)MathHelper.ToDegrees(m_currentGameObject.rotation.Z);
                    nudRotationX.Visible = true;
                    nudRotationY.Visible = true;
                    nudRotationZ.Visible = true;

                }
            }
        }

        private void btnUpdatePosition_Click(object sender, EventArgs e)
        {
            btnPositionChange.Text = "change";
            btnUpdatePosition.Visible = false;
            nudPositionX.Visible = false;
            nudPositionY.Visible = false;
            nudPositionZ.Visible = false;

            btnSpawn.Visible = false;
            nudRotationX.Visible = false;
            nudRotationY.Visible = false;
            nudRotationZ.Visible = false;

            m_currentGameObject.body.MoveTo(new Vector3((float)nudPositionX.Value, (float)nudPositionY.Value, (float)nudPositionZ.Value), 
                Matrix.CreateFromYawPitchRoll((float)MathHelper.ToRadians((float)nudRotationY.Value),
                (float)MathHelper.ToRadians((float)nudRotationX.Value),
                (float)MathHelper.ToRadians((float)nudRotationZ.Value)));
        }


        private void btnUpdateClick_Click(object sender, EventArgs e)
        {

        }

        //This function executes anytime you click on a tool strip item, the name is missleading ~~
        private void coinClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            btnSpawn.Visible = true;
            btnMovable.Visible = true;
            nudPositionX.Visible = true;
            nudPositionY.Visible = true;
            nudPositionZ.Visible = true;
            nudRotationX.Visible = true;
            nudRotationY.Visible = true;
            nudRotationZ.Visible = true;
            nudScaleX.Visible = true;
            nudScaleX.Value = 1;
            nudScaleY.Visible = true;
            nudScaleY.Value = 1;
            nudScaleZ.Visible = true;
            nudScaleZ.Value = 1;
            labelAlias.Visible = true;
            textBoxAlias.Visible = true;

            m_spawnObjectStaticFlag = false;
            m_powerUp_Coin_Parameter = "";
            btnMovable.Text = "Movable";
            switch (e.ClickedItem.Name)
            {
                case "coinToolStripMenuItem":
                    m_spawnObjectType = "Lloyd_coin";
                    m_powerUp_Coin_Parameter = "50";
                    break;
                case "barrel01ToolStripMenuItem":
                    m_spawnObjectType = "OilBarrel01";
                    break;
                case "barrel02ToolStripMenuItem":
                    m_spawnObjectType = "OilBarrel02";
                    break;
                case "debugboxToolStripMenuItem":
                    m_spawnObjectType = "Debugbox";
                    break;
                case "carToolStripMenuItem":
                    m_spawnObjectType = "Car";
                    break;
                case "plank01ToolStripMenuItem":
                    m_spawnObjectType = "plank01";
                    break;
                case "container01ToolStripMenuItem":
                    m_spawnObjectType = "container01";
                    break;
                case "woodcrateToolStripMenuItem":
                    m_spawnObjectType = "WoodCrate";
                    break;
                case "loadingdock01ToolStripMenuItem":
                    m_spawnObjectType = "loadingdock01";
                    break;
                case "wall01ToolStripMenuItem":
                    m_spawnObjectType = "Wall01";
                    break;
                case "powerUpNitroToolStripMenuItem":
                    m_spawnObjectType = "PowerUp";
                    m_powerUp_Coin_Parameter = "NITRO";
                    break;
                case "powerUpPunchToolStripMenuItem":
                    m_spawnObjectType = "PowerUp";
                    m_powerUp_Coin_Parameter = "PUNCH";
                    break;
                case "powerUpReverse_KeysToolStripMenuItem":
                    m_spawnObjectType = "PowerUp";
                    m_powerUp_Coin_Parameter = "REVERSE_KEYS";
                    break;
                case "powerUpSlowToolStripMenuItem":
                    m_spawnObjectType = "PowerUp";
                    m_powerUp_Coin_Parameter = "SLOW";
                    break;
                case "powerUpDoubletheirpointsToolStripMenuItem":
                    m_spawnObjectType = "PowerUp";
                    m_powerUp_Coin_Parameter = "DOUBLETHEIRPOINTS";
                    break;
                case "powerUpDoubleyourpointsToolStripMenuItem":
                    m_spawnObjectType = "PowerUp";
                    m_powerUp_Coin_Parameter = "DOUBLEYOURPOINTS";
                    break;
                case "powerUplow_frictiontoolStripMenuItem":
                    m_spawnObjectType = "PowerUp";
                    m_powerUp_Coin_Parameter = "LOW_FRICTION";
                    break;
                case "threadplatetoolStripMenuItem":
                    m_spawnObjectType = "threadplate";
                    break;
                case "brushStripMenuItem":
                    m_spawnObjectType = "brush";
                    break;
                case "canToolStripMenuItem":
                    m_spawnObjectType = "can_ligg";
                    break;
                case "lampToolStripMenuItem":
                    m_spawnObjectType = "lamp_1";
                    break;
                case "roofToolStripMenuItem":
                    m_spawnObjectType = "roof";
                    break;
                case "takstolToolStripMenuItem":
                    m_spawnObjectType = "takstol";
                    break;
            }
        }
        
        private void btnSpawn_Click(object sender, EventArgs e)
        {

            m_gameWorld.addGameObject(m_spawnObjectType,
                                      new Vector3((float)nudPositionX.Value, (float)nudPositionY.Value, (float)nudPositionZ.Value),
                                      new Vector3((float)nudRotationX.Value, (float)nudRotationY.Value, (float)nudRotationZ.Value),
                                      new Vector3((float)nudScaleX.Value, (float)nudScaleY.Value, (float)nudScaleZ.Value),
                                      m_spawnObjectStaticFlag,
                                      textBoxAlias.Text, m_powerUp_Coin_Parameter, Vector3.One);

            btnSpawn.Visible = false;
            btnMovable.Visible = false;
            nudPositionX.Visible = false;
            nudPositionY.Visible = false;
            nudPositionZ.Visible = false;
            nudRotationX.Visible = false;
            nudRotationY.Visible = false;
            nudRotationZ.Visible = false;
            nudScaleX.Visible = false;
            nudScaleY.Visible = false;
            nudScaleZ.Visible = false;
            labelAlias.Visible = false;
            textBoxAlias.Visible = false;
        }

        private void btnMovable_Click(object sender, EventArgs e)
        {
            if (btnMovable.Text == "Movable")
            {
                m_spawnObjectStaticFlag = true;
                btnMovable.Text = "Immovable";
            }
            else
            {
                m_spawnObjectStaticFlag = false;
                btnMovable.Text = "Movable";
            }
        }

    }
}
